<?php

namespace Admin\Controller;

use Admin\Model\UserModel;
use Think\Controller;

class UserController extends CommenController
{
    //列表函数
    public function get_list($field,$where,$order,$page_num){
        $db_user=M('user','','XYC');
        //权限
        $role=M('shopkeeper_role','','XYC')->where(['shop_identifier'=>$_SESSION['shop_identifier'],'sid'=>$_SESSION['id']])->getfield('rid',true);
        $w=[];
        $flag=0;

        if(in_array('3',$role)){//催收
            $w['b.collector']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('10',$role) && !in_array('1',$role)){//客服
            $w['a.chat_id']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('11',$role)){//审核员
            $w['b.approver']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('6',$role)){//渠道账号
            $w['a.channel']=$_SESSION['chan_id'];
            $flag=1;
        }

        if($flag==1){
            $w['_logic']='OR';
            $where['_complex']=$w;
        }

        $tol=$db_user->join('a join user_login b on b.user_id=a.id ')->where($where)->count();
        $row = '10';
        if(!empty($page_num)){
            $row=$page_num;
        }

        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = $db_user->where($where)->join('a join user_login b on b.user_id=a.id ')->join('left join channel c on c.chan_id=a.channel')->order($order)->where($where)->field($field)->limit($page->listfirst, $page->listRows)->distinct('a.id')->order('a.id desc')->select();

        $channel = M('channel', '', 'XYC')->where(['state' => array('neq', -1), 'shop_identifier' => $_SESSION['shop_identifier']])->field('chan_id,channel_name')->select();
        foreach ($data as $k=>$v){
          //渠道
          if (empty($v['channel_name']) || $v['channel_name'] = ''){
              $data[$k]['channel_name'] = '自增';
          }
          //脱敏
          if($_SESSION['is_hide']=='2'){
              $data[$k]['mobile']=set_privacy($v['mobile'],1);
              $data[$k]['identity_num']=set_privacy($v['identity_num'],2);
          }
      }
        $result=array('data'=>$data,'fpage'=>$fpage,'channel'=>$channel,'tol'=>$tol);
        return $result;
    }

    /**
     * 所有用户列表
     */
    public function user_list()
    {
//        $start = I('start');//开始时间；
//        $end = I('end');//结束时间
//        $start_time = strtotime($start);
//        $end_time = strtotime($end);
//        $cert_status = I('cert_status');//认证状态
//        $check_type = I('check_type');//审核类型
//        $channel = I('channel');// 渠道
//        $search = I('key');//搜索
        $show_num=I('show_num');
        $search_arr = array('key' => I('key'), 'start' => I('start'), 'end' => I('end'), 'cert_status' => I('cert_status'), 'check_type' => I('check_type'), 'channel' =>  I('channel'),'show_num'=>$show_num);
        $shop_identifier = $_SESSION['shop_identifier'];
        $db_user = M('user', '', 'XYC');
        //搜索字段
        $where = [];
        $where['a.is_delete']=1;
        $search_log='【用户管理-所有用户】搜索：';
        $is_search=0;
        if (!empty(I('start')) || !empty(I('end'))) {
             $start_time = strtotime(I('start'));
             $end_time = strtotime(I('end'));
            $is_search=1;
            if (empty(I('end'))) {
                $where['a.ctime'] = array('egt', $start_time);
            } else if (empty(I('start'))) {
                $where['a.ctime'] = array('elt', $end_time);
            } else {
                $where['a.ctime'] = array('between', array($start_time, $end_time));
            }
            $search_log=$search_log.' 注册时间为 '.I('start').'--'.I('end');
        }

        if (!empty(I('cert_status'))) {
            $cert_status=I('cert_status');
            $is_search=1;
            $where['a.cert_status'] = $cert_status;
            $cert_name='';
            switch ($cert_status){
                case 1: $cert_name='未认证'; break;
                case 2: $cert_name='认证通过'; break;
                case 3: $cert_name='认证拒绝'; break;
                case 4: $cert_name='其他认证中'; break;
                case 5: $cert_name='审核中'; break;
            }
            $search_log=$search_log.' 审核状态为 '.$cert_name;
        }

        if (!empty(I('check_type'))) {
            $check_type=I('check_type');
            $is_search=1;
            $where['a.check_type'] = $check_type;
            $type_name='';
             switch ($check_type){
                 case 1: $type_name='无操作'; break;
                 case 2: $type_name='机器审核'; break;
                 case 3: $type_name='人工审核'; break;
             }
         $search_log=$search_log.' 审核类型为 '.$type_name;
        }
//        if (!empty(I('channel'))) {
//            $channel=I('channel');
//            if($channel=='99'){//自增
//                $where['a.channel'] ='';
//            }else{
//                $where['a.channel'] = $channel;
//            }
//            $is_search=1;
//            $search_log=$search_log.' 渠道为 '.$channel;
//        }

        if (!empty(I('key'))) {
            $search=I('key');
            $is_search=1;
            $where['a.username|a.mobile|a.identity_num'] = array('like',$search . '%');
            $search_log=$search_log.' 关键字为 '.$search;
        }
        if($is_search==1){
            add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],$search_log,2);
        }
        $where['a.shop_identifier'] = $shop_identifier;

        //权限
        $role=M('shopkeeper_role','','XYC')->where(['shop_identifier'=>$_SESSION['shop_identifier'],'sid'=>$_SESSION['id']])->getfield('rid',true);
        $w=[];
        $flag=0;

        if(in_array('3',$role)){//催收
            $w['b.collector']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('10',$role) && !in_array('1',$role)){//客服
            $w['a.chat_id']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('11',$role)){//审核员
            $w['b.approver']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('6',$role)){//渠道账号
            $w['a.channel']=$_SESSION['chan_id'];
            $flag=1;
        }

        if($flag==1){
            $w['_logic']='OR';
            $where['_complex']=$w;
        }

        $tol = $db_user->join('a join user_login b on b.user_id=a.id ')->where($where)->COUNT();

        $row = '10';
        if(!empty($show_num)){
            $row=$show_num;
        }
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'a.mobile,a.username,a.identity_num,a.id,a.ctime,b.mobilefirm,c.channel_name,a.cert_status,b.approve_state,a.status,a.quota,a.balance';
        $data = $db_user->where($where)->join('a  join user_login b on b.user_id=a.id ')->join('left join channel c on c.chan_id=a.channel')->where($where)->field($field)->limit($page->listfirst, $page->listRows)->order('a.id desc')->select();

        //渠道
//        $channel = M('channel', '', 'XYC')->where(['state' => array('neq', -1), 'shop_identifier' => $shop_identifier])->field('chan_id,channel_name')->select();
        foreach ($data as $k=>$v){
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $cert_time=M('user_examine','','XYC')->where(['user_id'=>$v['id'],'type'=>1])->field('ctime')->order('id desc')->limit(1)->find();
           if(!empty($cert_time)){
               $data[$k]['cert_time'] =date('Y-m-d H:i:s', $cert_time['ctime']);
           }else{
               $data[$k]['cert_time'] ='--';
           }

//            //渠道
//            if (empty($v['channel_name']) || $v['channel_name'] = '') {
//                $data[$k]['channel_name'] = '自增';
//            }
            //1正常2锁定3拉黑
            switch ($v['status']){
                case 1:
                    $data[$k]['black_status'] = '正常';
                    break;
                case 2:
                    $data[$k]['black_status'] = '锁定';
                    break;
                case 3:
                    $data[$k]['black_status'] = '拉黑';
                    break;
            }
            //认证状态
            switch ($v['cert_status']) {
                case 1:
                    $data[$k]['cert_status'] = '未认证';
                    break;
                case 2:
                    $data[$k]['cert_status'] = '认证通过';
                    break;
                case 3:
                    $data[$k]['cert_status'] = '认证拒绝';
                    break;
                case 4:
                    $data[$k]['cert_status'] = '其他认证中';
                    break;
                case 5:
                    $data[$k]['cert_status'] = '审核中';
                    break;
                case 6:
                    $data[$k]['cert_status'] = '审核中';
                    break;
            }
            //脱敏
            if($_SESSION['is_hide']=='2'){
                $data[$k]['mobile']=set_privacy($v['mobile'],1);
                $data[$k]['identity_num']=set_privacy($v['identity_num'],2);
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'rid' => $_SESSION['id'],
            'search' => $search_arr,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     * 拉黑/解除用户
     */
    public function pull_black(){

        $type=I('type');//1拉黑 2 解除拉黑
        $u_id=I('id');
        $reason=I('reason');

        if($type=='1'){
            $update=M('user','','XYC')->where(['id'=>$u_id])->setField('status',3);
        }else if($type=='2'){
            $update=M('user','','XYC')->where(['id'=>$u_id])->setField('status',1);
        }else{
            $this->ajaxReturn(['code'=>'1002','msg'=>'操作有误']);
            die;
        }
        if($update){
            add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],'拉黑用户'.$u_id,3);
            $arr=['reason'=>$reason,'user_id'=>$u_id,'ctime'=>time(),'operator'=>$_SESSION['account'],'shop_identifier'=>$_SESSION['shop_identifier'],'type'=>$type];
            M('pull_black_record','','XYC')->add($arr);
            $this->ajaxReturn(['code'=>'1001','msg'=>'操作成功']);
        }else{
            $this->ajaxReturn(['code'=>'1002','msg'=>'操作失败']);
        }


    }

    /**
     * 用户调额
     */
    public function change_quota(){

        $quota=I('quota');
        $u_id=I('id');
//        $quota=intval($quota,2);
        if($quota<0 || $quota>10000  || !is_numeric($quota))
        {
            $this->ajaxReturn(['code'=>'1002','msg'=>'请输入合理的额度']);
        }
        $update=M('user','','XYC')->where(['id'=>$u_id])->setfield('quota',$quota);
        if($update){
            //已用余额
            $has_borrow=M('vz_loan','','XYC')->where(['user_id'=>$u_id,'status'=>['notin',[2,4]]])->sum('loan_amount');
            if(empty($has_borrow)){
                $has_borrow=0;
            }
            M('user','','XYC')->where(['id'=>$u_id])->setfield('balance',$quota-$has_borrow);

            add_control_log($_SESSION['shop_identifier'],$_SESSION['account'],'用户调额'.$quota.$u_id,3);
            $this->ajaxReturn(['code'=>'1001','msg'=>'操作成功']);
        }else{
            $this->ajaxReturn(['code'=>'1002','msg'=>'操作失败']);
        }
    }

    /**
     * 借款详情
     */
    public function user_borrow(){
      $u_id=I('id');
//      echo $u_id;
      $data=M('vz_loan','','XYC')->where(['user_id'=>$u_id,'is_success'=>2,'status'=>['in',[1,2,3,6]]])
                    //借款金额，订单号，       到账时间， 应还款时间，  实际还款时间，状态
          ->field('loan_amount,order_number,get_time,atlast_time,actual_time,status')->order('id desc')->select();

      foreach ($data as $k=>$v){

          $data[$k]['get_time']=date('Y-m-d',$v['get_time']);//到账时间
          $data[$k]['actual_time']=date('Y-m-d',$v['actual_time']);//还款时间
          $data[$k]['overdue_time']='--';   //逾期天数
          $data[$k]['overdue_money']='--';  //逾期金额
          if($v['actual_time']<=0){
              $data[$k]['actual_time']='--';
              $overdue = M('user_overdue','','XYC_SERVICE')
                  ->field('overdue_day,overdue_money')
                  ->where(['numbering'=>$v['order_number']])
//                  ->buildSql();
                  ->find();
//              echo $overdue;
              if(!empty($overdue)){
                  $data[$k]['overdue_time']=$overdue['overdue_day'];
                  $data[$k]['overdue_money']=$overdue['overdue_money'];
              }
          }
          switch ($v['status']){
              case 1: $data[$k]['status']='未还款'; break;
              case 2: $data[$k]['status']='已还款'; break;
              case 3: $data[$k]['status']='已逾期'; break;
              case 5: $data[$k]['status']='待打款'; break;
          }
      }

        $this->ajaxReturn(['code'=>'1001','result'=>$data]);

    }

    /***
     *用户详情
     */
    public function user_detail()
    {
        $id = I('u_id');
        $shop_identifier = $_SESSION['shop_identifier'];
        $cert_status=M('user','','XYC')->WHERE(['id'=>$id])->getField('cert_status');
        if($cert_status!='2' && $cert_status!='3' && $cert_status!='5' ){
            $this->error('该用户未完成认证，无法查看详情');
            die;
        }
        $key = md5($id . $shop_identifier . 'xyc_merchant');
        $url = 'http://det.xycpro.com/report/userdetails?user_id=' . $id . '&mer_id=' . $shop_identifier . '&key=' . $key.'&shop_id='.$_SESSION['id'];
        header('location:' . $url);
        $this->ajaxReturn(['code'=>'1001']);
        exit;
    }

    /**
     * 未认证会员列表 改
     */
    public function user_list_nocert()
    {
        $shop_identifier = $_SESSION['shop_identifier'];
//        $db_user = M('user', '', 'XYC');
        //搜索字段
        $search_arr = array('key' => I('key'), 'start' => I('start'), 'end' => I('end'), 'channel' =>  I('channel'));
        $where = [];
        $where['a.is_delete']=1;
        $search_log='【用户管理-未认证用户】搜索：';
        $is_search=0;
        if (!empty(I('start')) || !empty(I('end'))) {
            $start_time = strtotime(I('start'));
            $end_time = strtotime(I('end'));
            if (empty(I('end'))) {
                $where['a.ctime'] = array('egt', $start_time);
            } else if (empty(I('start'))) {
                $where['a.ctime'] = array('elt', $end_time);
            } else {
                $where['a.ctime'] = array('between', array($start_time, $end_time));
            }
            $is_search=1;
            $search_log=$search_log.' 注册时间为 '.I('start').'--'.I('end');
        }
        if (!empty(I('channel'))) {
            $channel=I('channel');
            $where['a.channel'] = $channel;
            $is_search=1;
            $search_log=$search_log.' 渠道为 '.$channel;
        }
        if (!empty(I('key'))) {
            $search=I('key');
            $is_search=1;
            $where['a.username|a.mobile|a.identity_num'] = array('like', $search . '%');
            $search_log=$search_log.' 关键字为 '.$search;
        }
         if($is_search==1){
             add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],$search_log,2);

         }
        $where['a.shop_identifier'] = $shop_identifier;
        $where['a.cert_status'] = 1;


        $field = 'a.mobile,a.username,a.id,a.ctime,b.mobilefirm,a.channel';

        $result=$this->get_list($field,$where,'a.id desc');

        $data=$result['data'];
        //出借人与渠道
        $channel=M('channel','','XYC')->join('a join lender b on b.id=a.lender_id')->where(['a.chan_id'=>['in',array_column($data,'channel')]])->getfield('a.chan_id,a.channel_name,b.name');
        foreach ($data as $k=>$v){
            //用户意向 根据借条的数据判断高中低
            $intent = M('user_jt', '', 'XYC')->where(['mobile' => $v['mobile']])->field('is_borrow,is_register')->find();
            if (!empty($intent) && $intent['is_register'] != '0') {
                if ($intent['is_borrow'] == '2') {
                    $data[$k]['intent'] = '高';
                } else {
                    $data[$k]['intent'] = '中';
                }
            } else {
                $data[$k]['intent'] = '低';
            }
            //机器预审
            switch ($v['checkstatus']){
                case  1: $data[$k]['checkstatus']='待审核';   break;
                case  2: $data[$k]['checkstatus']='审核中';   break;
                case  3: $data[$k]['checkstatus']='完成审核';   break;
            }

            //渠道和出借人
            $data[$k]['channel_name']=$channel[$v['channel']]['channel_name'];
            $data[$k]['lender']=$channel[$v['channel']]['name'];

        }
        $this->assign(array(
            'channel' => $result['channel'],
            'data' => $data,
            'fpage' => $result['fpage'],
            'search' => $search_arr,
            'tol' => $result['tol']
        ));
        $this->display();

    }

    /**
     * 已借款用户列表
     */
    public function user_list_borrow()
    {
        $application_time = I('application_time');//申请时间；
        $borrow_time = I('borrow_time');//审核时间
        $channel = I('channel');// 渠道
        $search = I('key');//搜索
        $show_num=I('show_num');
        $shop_identifier = $_SESSION['shop_identifier'];
        //分割时间
        $apply_arr = explode(' - ', $application_time);
        $b_arr = explode(' - ', $borrow_time);
        $search_log='【用户管理-已借款用户】搜索：';
        $is_search=0;
        $where = [];
        if (!empty($application_time)) {
            $a_start = strtotime($apply_arr[0]);
            $a_end = strtotime($apply_arr[1]);
            $where['b.ctime'] = array('between', array($a_start, $a_end));
            $search_log=$search_log.' 申请时间为 '.$application_time;
            $is_search=1;
        }

        if (!empty($borrow_time)) {
            $b_start = strtotime($b_arr[0]);
            $b_end = strtotime($b_arr[1]);
            $where['d.ctime'] = array('between', array($b_start, $b_end));
            $search_log=$search_log.' 借款时间为 '.$application_time;
            $is_search=1;

        }

        $db_user = M('user', '', 'XYC');
        //搜索字段
        $search_arr = array('key' => $search, 'application_time' => $application_time, 'borrow_time' => $borrow_time, 'channel' => $channel,'show_num'=>$show_num);
        if (!empty($channel)) {
            if ($channel == '0') {
                $where['a.channel'] = array('exp', 'is null');
            } else {
                $where['a.channel'] = $channel;
            }
            $search_log=$search_log.' 渠道为 '.$channel;
            $is_search=1;
        }
        if (!empty($search)) {
            $where['a.username|a.mobile|a.identity_num'] = array('like', '%' . $search . '%');
            $search_log=$search_log.' 关键字为 '.$search;
            $is_search=1;
        }
        //日志记录
        if($is_search==1){
            add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],$search_log,2);
        }
        $where['a.shop_identifier'] = $shop_identifier;
//        $where['a.cert_status'] = 3;
        //权限
        $role=M('shopkeeper_role','','XYC')->where(['shop_identifier'=>$_SESSION['shop_identifier'],'sid'=>$_SESSION['id']])->getfield('rid',true);

        $w=[];
        $flag=0;

        if(in_array('3',$role)){//催收
            $w['b.collector']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('10',$role)){//客服
            $w['a.chat_id']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('11',$role)){//审核员
            $w['b.approver']=$_SESSION['id'];
            $flag=1;
        }

        if(in_array('6',$role)){//渠道账号
            $w['a.channel']=$_SESSION['chan_id'];
            $flag=1;
        }

        if($flag==1){
            $w['_logic']='OR';
            $where['_complex']=$w;
        }

        $tol = $db_user->alias('a')->join('left join user_login b on b.user_id=a.id ')->join('join vz_loan d on d.user_id=a.id and d.is_success=2')->where($where)->COUNT('DISTINCT a.id');
        $row = '10';
        if(!empty($show_num)){
            $row=$show_num;
        }
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'a.id as user_id,a.mobile,a.username,a.ctime,a.quota,a.balance';
        $data = $db_user->where($where)->join('a left join user_login b on b.user_id=a.id ')->join(' left join channel c on c.chan_id=a.channel')->join('join vz_loan d on d.user_id=a.id and d.is_success=2')->field($field)->limit($page->listfirst, $page->listRows)->order('a.id desc')->group('a.id')->select();


        foreach ($data as $k=>$v){

               $data[$k]['borrow_state']='未借款';//1 未借款 2 逾期 3 借款中
               //借款状态  -逾期
               $overdue=M('vz_loan','','XYC')->where(['user_id'=>$v['user_id'],'status'=>3,'actual_time'=>['eq',0]])->find();
               if($overdue){
                   $data[$k]['borrow_state']='逾期';
               }else{
                   //借款状态  -未还款
                   $borrowing=M('vz_loan','','XYC')->where(['user_id'=>$v['user_id'],'status'=>1,'actual_time'=>['eq',0]])->find();
                   if($borrowing){
                       $data[$k]['borrow_state']='借款中';
                   }
               }

            //已用余额
            $data[$k]['has_borrow']=M('vz_loan','','XYC')->where(['user_id'=>$v['user_id'],'status'=>['notin',[2,4]]])->sum('loan_amount');
            //脱敏
            if($_SESSION['is_hide']=='2'){
                $data[$k]['mobile']=set_privacy($v['mobile'],1);
                $data[$k]['identity_num']=set_privacy($v['identity_num'],2);
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'search' => $search_arr,
            'tol' => $tol
        ));
        $this->display();
    }
    /**
     * 审核中用户列表
     */
    public function user_list_examining()
    {
        $application_time = I('application_time');//申请时间；
        $cert_time = I('cert_time');//审核时间
        $channel = I('channel');// 渠道
        $search = I('key');//搜索
        $shop_identifier = $_SESSION['shop_identifier'];
        $show_num = I('show_num');
        //分割时间
        $apply_arr = explode(' - ', $application_time);
        $cert_arr = explode(' - ', $cert_time);
        $search_log = '【用户管理-待审核用户】搜索：';
        $is_search = 0;
        $where = [];
        $where['a.is_delete'] = 1;
        if (!empty($application_time)) {
            $a_start = strtotime($apply_arr[0]);
            $a_end = strtotime($apply_arr[1]);

            if($a_start==$a_end){
                $a_end=strtotime(date('Y-m-d 23:59:59',$a_start));
            }

            $where['b.ctime'] = array('between', array($a_start, $a_end));
            $is_search = 1;
            $search_log = $search_log . ' 申请时间为 ' . $application_time;
        }
        if (!empty($cert_time)) {
            $c_start = strtotime($cert_arr[0]);
            $c_end = strtotime($cert_arr[1]);
            $where['b.approve_time'] = array('between', array($c_start, $c_end));
            $is_search = 1;
            $search_log = $search_log . ' 审核时间为 ' . $cert_time;
        }

        $db_user = M('user', '', 'XYC');
        //搜索字段
        $search_arr = array('key' => $search, 'application_time' => $application_time, 'cert_time' => $cert_time, 'channel' => $channel, 'show_num' => $show_num);
        if (!empty($channel)) {
            if ($channel == '0') {
                $where['a.channel'] = array('eq', '');
            } else {
                $where['a.channel'] = $channel;
            }
            $is_search = 1;
            $search_log = $search_log . ' 渠道为 ' . $channel;
        }

        if (!empty($search)) {
            $where['a.username|a.mobile|a.identity_num'] = array('like', '%' . $search . '%');
            $is_search = 1;
            $search_log = $search_log . ' 关键字为 ' . $search;
        }
        if ($is_search == 1) {
            add_control_log($_SESSION['shop_identifier'], $_SESSION['name'], $search_log, 2);
        }
        $where['a.shop_identifier'] = $shop_identifier;
        $where['a.cert_status'] = 5;

        //权限
        $role = M('shopkeeper_role', '', 'XYC')->where(['shop_identifier' => $_SESSION['shop_identifier'], 'sid' => $_SESSION['id']])->getfield('rid', true);

        $w = [];
        $flag = 0;
        if (in_array('3', $role)) {//催收
            $w['b.collector'] = $_SESSION['id'];
            $flag = 1;
        }

        if (in_array('4', $role) && !in_array('1', $role)) {//客服
            $w['a.chat_id'] = $_SESSION['id'];
            $flag = 1;
        }

        if (in_array('11', $role)) {//审核员
            $w['b.approver'] = $_SESSION['id'];
            $flag = 1;
        }

        if (in_array('6', $role)) {//渠道账号
            $w['a.channel'] = $_SESSION['chan_id'];
            $flag = 1;
        }

        if ($flag == 1) {
            $w['_logic'] = 'OR';
            $where['_complex'] = $w;
        }

        $tol = $db_user->alias('a')->join('left join user_login b on b.user_id=a.id ')->where($where)->COUNT();
        $row = '10';
        if (!empty($show_num)) {
            $row = $show_num;
        }
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'a.id,a.mobile,a.username,a.identity_num,a.id,a.ctime,a.checkstatus,b.mobilefirm,c.channel_name,a.cert_status,b.approve_state,b.approver,b.ctime as commit_time,b.approve_time,c.lender_id';
        $data = $db_user->where($where)->join('a left join user_login b on b.user_id=a.id ')->join(' left join channel c on c.chan_id=a.channel')->field($field)->limit($page->listfirst, $page->listRows)->order('a.id desc')->select();
        //渠道
        $channel = M('channel', '', 'XYC')->where(['state' => array('neq', -1), 'shop_identifier' => $shop_identifier])->field('chan_id,channel_name')->select();
//        $data = $this->for_data($data);

         //借款人
          $lender=M('lender','','XYC')->where(['id'=>['in',array_column($data,'lender_id')]])->getfield('id,name',true);

        foreach ($data as $k => $v) {
            //渠道
            if (empty($v['channel_name']) || $v['channel_name'] = '') {
                $data[$k]['channel_name'] = '自增';
            }
            //审核人
            if (empty($v['approver'])) {
                $data[$k]['approver'] = '--';
            } else {
                $data[$k]['approver'] = M('shopkeeper', '', 'XYC')->where(['id' => $v['approver']])->getfield('name');
            }
            //审核时间
            if (!empty($v['approve_time']) && $v['approve_time'] > 10) {
                $data[$k]['approve_time'] = date('Y-m-d H:i:s', $v['approve_time']);

            } else {
                $data[$k]['approve_time'] = '--';
            }
            $data[$k]['commit_time'] = date('Y-m-d H:i:s', $v['commit_time']);

            //机器预审
            switch ($v['checkstatus']) {
                case  1:
                    $data[$k]['checkstatus'] = '待审核';
                    break;
                case  2:
                    $data[$k]['checkstatus'] = '审核中';
                    break;
                case  3:
                    $data[$k]['checkstatus'] = '完成审核';
                    break;
            }
            //借款状态
            switch ($v['approve_state']) {
                case 0:
                    $data[$k]['approve_state'] = '正常';
                    break;
                case 1:
                    $data[$k]['approve_state'] = '失联';
                    break;
                case 2:
                    $data[$k]['approve_state'] = '拒绝受理';
                    break;
                case 3:
                    $data[$k]['approve_state'] = '取消';
                    break;
                case 4:
                    $data[$k]['approve_state'] = '限制申请';
                    break;
                case 5:
                    $data[$k]['approve_state'] = '限制借款';
                    break;
            }
            //脱敏
            if ($_SESSION['is_hide'] == '2') {
                $data[$k]['mobile'] = set_privacy($v['mobile'], 1);
                $data[$k]['identity_num'] = set_privacy($v['identity_num'], 2);
            }
            //借款状态
            $data[$k]['borrow_status'] = $v['approve_state'];
            $data[$k]['lender'] = $lender[$v['lender_id']];

        }

        $this->assign(array(
            'channel' => $channel,
            'data' => $data,
            'fpage' => $fpage,
            'search' => $search_arr,
            'tol' => $tol
        ));
        $this->display();
    }
    /***
     * 用户审核分配
     */
    public function user_distribute()
    {
        $user_id = I('user_id');
        $user_arr = explode(',', $user_id);
        if (empty($_POST)) {
            $user = M('user', '', 'XYC')->where(['id' => array('in', $user_arr)])->select();
            //具有审核权限的子账号
//            $check=M('shopkeeper','','XYC')->where(['rid'=>7])->field('id,name')->select();
            $check = M('shopkeeper', '', 'XYC')->join('a join shopkeeper_role b on b.sid=a.id and shop_identifier= ' . $_SESSION['shop_identifier'])->where(['b.rid' => 11])->field('a.id,a.name')->select();
            $this->assign(array('user' => $user, 'check' => $check, 'user_ids' => $user_id));
            $this->display();
            die;
        }
        $check_id = I('check_id');
        $arr = array('approver' => $check_id);
        $update = M('user_login', '', 'XYC')->where(['user_id' => array('in', $user_arr)])->save($arr);
        if ($update) {
            add_control_log($_SESSION['shop_identifier'], $_SESSION['name'], '【用户管理-待审核用户】分配用户-' . $user_id, 3);
            $this->ajaxReturn(array('code' => '1001', 'result' => '分配成功'));
            die;
        }
        $this->ajaxReturn(array('code' => '1002', 'result' => '分配失败'));
        die;


    }



}